﻿/*
 * Created by SharpDevelop.
 * User: Dror B. Helper
 * Date: 15/09/2009
 * Time: 22:26
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;

namespace StackOverflowNotifier2
{
    /// <summary>
    /// Description of MyClass.
    /// </summary>
    public class Parser2
    {
        public const string badgeRegexFormatString = "class=\"badge{0}\".*?class=\"badgecount\".*?>(?<BadgeCount>.*?)<";

        public static Regex regexName = new Regex(
            "<span class=\"username\"><img src=\".*?><a href=\".*?>(?<Name>.*?)</a></span>",
            RegexOptions.CultureInvariant | RegexOptions.Compiled);

        public static Regex regexReputation = new Regex(
            "class=\"reputation-score\".*?>(?<Reputation>.*?)<",
            RegexOptions.CultureInvariant | RegexOptions.Compiled);

        public static Regex regexGoldBadages = new Regex(
            string.Format(badgeRegexFormatString, 1),
            RegexOptions.CultureInvariant | RegexOptions.Compiled);

        public static Regex regexSilverBadages = new Regex(
            string.Format(badgeRegexFormatString, 2),
            RegexOptions.CultureInvariant | RegexOptions.Compiled);

        public static Regex regexBronzeBadages = new Regex(
            string.Format(badgeRegexFormatString, 3),
            RegexOptions.CultureInvariant | RegexOptions.Compiled);

        public static Regex regexImage = new Regex(
      "<div class=\"gravatar\">\\s*<.*img.*src=\"(?<Image>.*?)\"",
    RegexOptions.CultureInvariant | RegexOptions.Compiled);

        public string ParseName(string inputText)
        {
            var m = regexName.Match(inputText);

            return m.Groups["Name"].Value;
        }

        public int ParseReputation(string inputText)
        {
            var m = regexReputation.Match(inputText);
            var repString = m.Groups["Reputation"].Value;

            return int.Parse(repString, NumberStyles.AllowThousands);
        }

        public string ParseImage(string inputText)
        {
             var m = regexImage.Match(inputText);

             return m.Groups["Image"].Value;
        }

        public enum BadgeType
        {
            Gold = 1,
            Silver = 2,
            Bronze = 3
        }

        public int ParseBandgeCount(string inputText, BadgeType badgeType)
        {
            Match m = null;
            switch (badgeType)
            {
                case BadgeType.Silver:
                    m = regexSilverBadages.Match(inputText);
                    break;
                case BadgeType.Bronze:
                    m = regexBronzeBadages.Match(inputText);
                    break;
                case BadgeType.Gold:
                    m = regexGoldBadages.Match(inputText);
                    break;
            }

            var badgeCountString = m.Groups["BadgeCount"].Value;
            if (string.IsNullOrEmpty(badgeCountString))
            {
                return 0;
            }
            return int.Parse(badgeCountString, NumberStyles.AllowThousands);
        }
    }
}